from django.shortcuts import render, redirect
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from app.models import order, dish_ingre, ingredients, EmployeeUser, message, dishes
from app.views import myLog_decrator


# Create your views here.
@login_required
def kitchenHome(request):
    orderlist = order.objects.all()
    listData = []
    for theorder in orderlist:
        tem = {}
        thedishes = theorder.dishesid
        dish_ingrelist = dish_ingre.objects.filter(DI_dish=thedishes)
        tem["id"] = theorder.id
        tem["order_time"] = theorder.order_time
        tem["clientid"] = theorder.clientid.name
        tem["dishesid"] = theorder.dishesid.name
        tem["operatorid"] = theorder.operatorid.name
        tem["tableid"] = theorder.tableid.id
        tem["done"] = theorder.done
        for record in dish_ingrelist:
            num = record.DI_Num
            ingr = record.DI_ingr
            if ingr.num < num:
                tem["warn"] = True
                break
            pass
        listData.append(tem)
        pass
    dictData = {"data": listData, "ingrlist": ingredients.objects.all(), "messlist": message.objects.all()}
    return render(request, 'kitchenHome.html', dictData)
    pass


@login_required
def done(request, theid):
    Theorder = order.objects.get(id=theid)
    thedishes = Theorder.dishesid
    dish_ingrelist = dish_ingre.objects.filter(DI_dish=thedishes)
    for record in dish_ingrelist:
        num = record.DI_Num
        ingr = record.DI_ingr
        ingr.num = ingr.num - num
        ingr.save()
        Theorder.done = True
    Theorder.save()
    # 写入日志
    myLog_decrator(request,"ID为{0}订单的菜品完成".format(theid))
    return redirect('/kitchenHome/')
    pass

@login_required
def addingr(request, ingrid):
    num = int(request.POST.get('num'))
    if num > 0:
        ingr = ingredients.objects.get(id=ingrid)
        ingr.num = ingr.num + int(num)
        ingr.save()
        # 写入日志
        myLog_decrator(request, "入库ID为{0}的食材{1}".format(ingrid, num))
        pass
    return redirect('/kitchenHome/')
    pass


@login_required
def call(request, orderid):
    opid = request.session.get('_auth_user_id')
    theorder = order.objects.get(id=orderid)
    targetId = theorder.operatorid
    sourceId = EmployeeUser.objects.get(id=opid)
    txt = "服务员{0}请到后厨领取{1}送至{2}号桌,信息来自{3}".format(targetId.name, theorder.dishesid.name, theorder.tableid.id,
                                                  sourceId.name)
    message.objects.create(txt=txt, sourceId=sourceId, targetId=targetId)
    # 写入日志
    myLog_decrator(request,"发送信息至{0}提醒取{1}".format(targetId.name,theorder.dishesid.name))
    return redirect('/kitchenHome/')
    pass


@login_required
def newingr(request):
    ingr = request.POST.get('ingr')
    num = request.POST.get('num')
    ingredients.objects.create(name=ingr, num=num)
    # 写入日志
    myLog_decrator(request,"添加新食材{0}入库数量{1}".format(ingr,num))
    return redirect('/kitchenHome/')
    pass


@login_required
def adddishes(request):
    if request.POST:
        ingrList = request.POST.getlist('ingr')
        name = request.POST.get('name')
        price = request.POST.get('price')
        thedishes = dishes.objects.create(name=name, price=price)
        for i in ingrList:
            ingnum = request.POST.get('ingrnum' + i)
            ingr = ingredients.objects.get(id=i)
            dish_ingre.objects.create(DI_ingr=ingr, DI_dish=thedishes, DI_Num=ingnum)
            pass
        # 写入日志
        myLog_decrator(request, "添加新菜品{0}价格{1}".format(name, price))
        return render(request, 'adddishes.html', {"ingrlist": ingredients.objects.all(), 'msg': '添加成功'})
        pass
    else:
        return render(request, 'adddishes.html', {"ingrlist": ingredients.objects.all()})
    pass


@login_required
def checkDishesName(request):
    resule = {}
    name = request.POST.get('name')
    if dishes.objects.filter(name=name):
        resule["resule"] = "不可用"
        pass
    else:
        resule["resule"] = "可用"
        pass
    return JsonResponse(resule)
    pass
